//********************************************************************************************************
// Product Name: MapWindow.dll Alpha
// Description:  The basic module for MapWindow version 6.0
//********************************************************************************************************
// The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); 
// you may not use this file except in compliance with the License. You may obtain a copy of the License at 
// http://www.mozilla.org/MPL/ 
//
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF 
// ANY KIND, either expressed or implied. See the License for the specificlanguage governing rights and 
// limitations under the License. 
//
// The Original Code is from MapWindow.dll version 6.0
//
// The Initial Developer of this Original Code is Ted Dunsford. Created 6/25/2008 11:27:09 AM
// 
// Contributor(s): (Open source contributors should list themselves and their modifications here). 
//
//********************************************************************************************************

using System;
using System.Drawing;
using Microsoft.DirectX.Direct3D;
using MapWindow.Drawing;
using MapWindow.Geometries;

namespace MapWindow.DirectX
{


    /// <summary>
    /// TexturedWedgeList
    /// </summary>
    public class TexturedCapListDX : PrimitiveBaseDX
    {
        #region Private Variables

        ILineSymbolizer _symbolizer;

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of TexturedWedgeList
        /// </summary>
        public TexturedCapListDX(ILineSymbolizer inSymbolizer)
        {
            _symbolizer = inSymbolizer;
        }

        #endregion

        #region Methods

        
        /// <summary>
        /// Adds a single cap based on the position of the center
        /// </summary>
        /// <param name="center"></param>
        public void AddCap(Coordinate center)
        {
            double mZ = Convert.ToSingle(center.Z);
            if (double.IsNaN(mZ)) mZ = 0.0;
            Vector vCenter = new Vector(center.X, center.Y, mZ);
            double width = _symbolizer.GetWidth();
            Vector dx = new Vector(1, 0, 0).Multiply(width / 2);
            Vector dy = new Vector(0, 1, 0).Multiply(width / 2);
            Vector topLeft = vCenter.Subtract(dx).Add(dy);
            Vector topRight = vCenter.Add(dx).Add(dy);
            Vector bottomLeft = vCenter.Subtract(dx).Subtract(dy);
            Vector bottomRight = vCenter.Add(dx).Subtract(dy);


            Vertices.AddCoordinate(topLeft);
            Vertices.AddCoordinate(topRight);
            Vertices.AddCoordinate(bottomLeft);
            Vertices.AddCoordinate(bottomRight);

           

        }

        
      
        
        /// <summary>
        /// Initializes the texture if it hasn't been set up yet.
        /// </summary>
        /// <param name="e">A DrawArgsDX event argument</param>
        protected override void OnInitialize(DrawArgsDX e)
        {
            if (_symbolizer == null) return;
            if (TextureComp == null) TextureComp = new TextureComponent();
            Bitmap bmp = DrawCapCircle(e);
            //bmp.Save("C:\\testCap.bmp");
            TextureComp.TextureDX = new Texture(e.Device, bmp, Usage.AutoGenerateMipMap, Pool.Managed);

            if (Indices == null || Indices.Count == 0)
            {
                Indices = new IndexCollection(Vertices.Count / 4);
            }
            
            base.OnInitialize(e);
        }

        /// <summary>
        /// For textured caps, rather than using several triangles for the caps,
        /// we simply use a transparent square with a circle drawn on the square.
        /// </summary>
        /// <returns></returns>
        protected virtual Bitmap DrawCapCircle(DrawArgsDX args)
        {
            double width = _symbolizer.GetWidth();
            Bitmap bmp = new Bitmap((int)width, (int)width);
            Graphics g = Graphics.FromImage(bmp);
            int w = (int)width - 1;
            Rectangle r = new Rectangle(0, 0, w, w);
            g.Clear(Color.Transparent);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Brush b = new SolidBrush(_symbolizer.GetFillColor());
            g.FillEllipse(b, r);
            b.Dispose();
            g.Dispose();
            return bmp;
        }


        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the symbolizer to use
        /// </summary>
        public ILineSymbolizer Symbolizer
        {
            get { return _symbolizer; }
            set { _symbolizer = value; }
        }
      
       

        #endregion



    }
}
